﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using DevExpress.LocalizationCHS;
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;

using DevExpress.XtraEditors.Repository;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using JsalesModel;


namespace JSales.ModuleProduct
{
    public partial class KindSetForm : Form
    {
        public KindSetForm()
        {
            InitializeComponent();
        }

        public BindingSource bindingSource = new BindingSource();

        BindingList<TbPtKind> bindingList= new BindingList<TbPtKind>();
        private string EditMode = null;

        private void CtrolINI()
        {
            barButtonAdd.Enabled = true;
            barButtonEdit.Enabled = true;
            barButtonDelete.Enabled = true;
            barButtonSave.Enabled = false;
            barButtonCancel.Enabled = false;

            tbProductID.Enabled = false;
            tbProductName.Enabled = false;
            checkDisable.Enabled = false;

            gridView1.CancelSelection();
        }

        private void KindSetForm_Load(object sender, EventArgs e)
        {
            CtrolINI();
            bindingList.AllowNew = true;
            bindingList.AllowEdit = true;
            bindingList.RaiseListChangedEvents = true;
            //汉化界面
            //DevExpress.XtraGrid.Localization.GridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraGridLocalizationCHS();
            //DevExpress.XtraTreeList.Localization.TreeListLocalizer.Active= new DevExpressXtraTreeListLocalizationCHS();
            GlobalVar.SelectKind();
            
            bindingList.Clear();
            foreach (TbPtKind kd in GlobalVar.GKinds.ToList())
            {
                bindingList.Add(kd);
            }

            

            //bindingSource.DataSource = GlobalVar.GKinds.ToList(); 
            gridControl1.DataSource = bindingList;



            gridView1.Columns["KindID"].Caption="品种编号";
            gridView1.Columns["KindName"].Caption="品种";
            gridView1.Columns["ISDELETED"].Caption = "停用标志";
            gridView1.Columns["LastEditDate"].Visible = false;
            gridView1.Columns["CreationDate"].Visible = false;
            gridView1.Columns["ID"].Visible = false;
        }
               
        private void BarButtonAddItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EditMode = "Add";

            barButtonSave.Enabled = true;
            barButtonCancel.Enabled = true;
            barButtonEdit.Enabled = false;
            barButtonDelete.Enabled = false;
            barButtonAdd.Enabled = false;

            tbProductID.Enabled = true;
            tbProductName.Enabled = true;

            gridView1.CancelSelection();
            tbProductID.Text = null;
            tbProductName.Text = null;
            checkDisable.CheckState = CheckState.Unchecked ;

            this.gridView1.OptionsBehavior.Editable = true;
            this.gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;
            gridView1.AddNewRow();
        }

        private void barButtonEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EditMode = "Edit";

            barButtonSave.Enabled = true;
            barButtonCancel.Enabled = true;
            barButtonEdit.Enabled = false;
            barButtonDelete.Enabled = false;
            barButtonAdd.Enabled = false;

            //tbProductID.Enabled = true;
            tbProductName.Enabled = true;
        }

        private void barButtonSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dbConText = new JYEntities();
            
            switch (EditMode)
                {
                    case "Edit":
                        {
                            int selectedHandle;
                            selectedHandle = this.gridView1.GetSelectedRows()[0];

                            if (!(string.IsNullOrEmpty(gridView1.GetRowCellValue(selectedHandle, "ID").ToString())))
                            {
                                IQueryable<TbPtKind> kinds = from i in dbConText.TbPtKinds
                                                             where i.ID == Int32.Parse(gridView1.GetRowCellValue(selectedHandle, "ID").ToString())
                                                             select i;
                                if (kinds.Any())
                                {
                                    TbPtKind kind = kinds.FirstOrDefault();
                                    kind.KindID = tbProductID.Text;
                                    kind.KindName = tbProductName.Text;

                                    dbConText.SaveChanges();
                                }
                            }
                            else
                            {
                                MessageBox.Show("品种编号、名称不能为空！");
                            }
                            break;
                        }
                    case "Add":
                        {

                            IQueryable<TbPtKind> kindExists = from i in dbConText.TbPtKinds
                                                              where i.KindID == tbProductID.Text
                                                              select i;
                            if (!kindExists.Any())
                            {
                                TbPtKind kind = new TbPtKind();
                                kind.KindID = tbProductID.Text;
                                kind.KindName = tbProductName.Text;
                                kind.ISDELETED = false;
                                dbConText.Add(kind);
                                dbConText.SaveChanges();
                                
                            }
                            else
                            {
                                MessageBox.Show("编号:\"" + kindExists.FirstOrDefault().KindID + ":" + kindExists.FirstOrDefault().KindName + "\"已存在！");
                            }
                            break;
                        }
                }
                KindSetForm_Load(sender, e);
                CtrolINI();
            
            KindSetForm_Load(sender,e);
        }

        private void barButtonCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CtrolINI();
        }

        private void barButtonDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int selectedHandle;
            selectedHandle = this.gridView1.GetSelectedRows()[0];

            if (!(string.IsNullOrEmpty(gridView1.GetRowCellValue(selectedHandle, "ID").ToString())))
            {
                var dbConText = new JYEntities();
                IQueryable<TbPtKind> kinds = from i in dbConText.TbPtKinds
                                             where i.ID == Int32.Parse(gridView1.GetRowCellValue(selectedHandle, "ID").ToString())
                                             select i;
                if (kinds.Any())
                {
                    TbPtKind kind = kinds.FirstOrDefault();
                    kind.ISDELETED = true;
                    dbConText.SaveChanges();
                    KindSetForm_Load(sender, e);
                }
            }
            else
            {
                MessageBox.Show("请先选择品种！");
            }
            KindSetForm_Load(sender,e);
        }

        private void dataGridViewKind_SelectionChanged(object sender, EventArgs e)
        {
            tbProductID.DataBindings.Clear();
            tbProductID.DataBindings.Add("Text", bindingSource, "KindID");

            tbProductName.DataBindings.Clear();
            tbProductName.DataBindings.Add("Text", bindingSource, "KindName");

            checkDisable.DataBindings.Clear();
            checkDisable.DataBindings.Add("Checked", bindingSource, "IsDeleted");

//            dataGridViewKind.Refresh();
        }

        private void gridView1_RowClick(object sender, RowClickEventArgs e)
        {
            tbProductID.DataBindings.Clear();
            tbProductID.DataBindings.Add("Text", bindingList, "KindID");

            tbProductName.DataBindings.Clear();
            tbProductName.DataBindings.Add("Text", bindingList, "KindName");

            checkDisable.DataBindings.Clear();
            checkDisable.DataBindings.Add("Checked", bindingList, "IsDeleted");

            gridControl1.Refresh();
        }

    }
}
